package cn.sinata.wanglin.village.fragment;

import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.xilada.xldutils.activitys.ImagePagerActivity;
import com.xilada.xldutils.adapter.BaseRecyclerAdapter;
import com.xilada.xldutils.adapter.util.SpacesItemDecoration;
import com.xilada.xldutils.adapter.util.ViewHolder;
import com.xilada.xldutils.fragment.BaseLazyFragment;
import com.xilada.xldutils.utils.ActivityUtil;
import com.xilada.xldutils.utils.SharedPreferencesUtils;
import com.xilada.xldutils.utils.Utils;

import java.util.ArrayList;

import butterknife.BindView;
import cn.sinata.wanglin.R;
import cn.sinata.wanglin.commonutils.Constant;
import cn.sinata.wanglin.model.Imageinfo;
import cn.sinata.wanglin.model.Lableinfo;
import cn.sinata.wanglin.model.Userinfo;
import cn.sinata.wanglin.net.HttpManager;
import cn.sinata.wanglin.net.model.ResultData;
import cn.sinata.wanglin.net.model.util.ResultDataSubscriber;
import cn.sinata.wanglin.village.activity.AllUserInfoActivity;
import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action0;
import rx.schedulers.Schedulers;

/**
 * description：
 * Created by kaihongwu on 2017/01/03 上午 10:05
 */
public class FriendPersonInfoFragment extends BaseLazyFragment implements AppBarLayout.OnOffsetChangedListener{
    @BindView(R.id.mSwipeRefreshLayout)
    SwipeRefreshLayout mSwipeRefreshLayout;
    //    认证列表 (1:有房认证，2：有车认证，3:学历认证，4:事业认证)
    @BindView(R.id.iv_approve1)
    ImageView iv_approve1;
    @BindView(R.id.iv_approve2)
    ImageView iv_approve2;
    @BindView(R.id.iv_approve3)
    ImageView iv_approve3;
    @BindView(R.id.iv_approve4)
    ImageView iv_approve4;
    @BindView(R.id.tv_birthday_fragment)
    TextView tv_birthday_fragment;//出身年月日
    @BindView(R.id.tv_industry)
    TextView tv_industry;//行业
    @BindView(R.id.tv_sign)
    TextView tv_sign;//签名
    @BindView(R.id.tv_belonglable)
    TextView tv_belonglable;//归属标签
    @BindView(R.id.rv_alllable)
    RecyclerView rv_alllable;//所有的标签
    @BindView(R.id.rv_allphoto)
    RecyclerView rv_allphoto;//所有的相册
    BaseRecyclerAdapter allphotoAdapter;
    BaseRecyclerAdapter alllableAdapter;
    private ArrayList<Lableinfo> malllabledatas=new ArrayList<Lableinfo>();
    private ArrayList<Imageinfo> mImageinfos=new ArrayList<Imageinfo>();
    private String friendID=null;
    @Override
    protected int getContentViewLayoutID() {
        return R.layout.fragment_otherinfo_isfriend;
    }
    public static FriendPersonInfoFragment newInstance(String friendID){
        FriendPersonInfoFragment mFriendPersonInfoFragment=new FriendPersonInfoFragment();
        Bundle bundle = new Bundle();
        bundle.putString("friendID",friendID);
        mFriendPersonInfoFragment.setArguments(bundle);
        return mFriendPersonInfoFragment;
    }
    @Override
    protected void onFirstVisibleToUser() {

        addOnOffsetChangeListener();

        mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                getUserinfo();
            }
        });

        if(alllableAdapter==null) {
            GridLayoutManager mgr = new GridLayoutManager(context, 3);
            rv_alllable.setLayoutManager(mgr);
            alllableAdapter = new BaseRecyclerAdapter<Lableinfo>(malllabledatas, R.layout.item_personalllable) {
                @Override
                public void onBind(final int position, final Lableinfo data, ViewHolder holder) {
                    holder.setText(R.id.tv_mylable, data.getName());
                }
            };
            rv_alllable.setAdapter(alllableAdapter);
        }
        if(allphotoAdapter==null){
            rv_allphoto.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false));
            rv_allphoto.addItemDecoration(new SpacesItemDecoration(3));
            allphotoAdapter= new BaseRecyclerAdapter<Imageinfo>(mImageinfos, R.layout.item_myimage_grid_nodelete) {
                @Override
                public void onBind(final int position, final Imageinfo data, ViewHolder holder) {
                    ((SimpleDraweeView) holder.bind(R.id.image)).setImageURI(data.getImgUrl());
                }
            };
            allphotoAdapter.setOnItemClickListener(new BaseRecyclerAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(View view, int position) {
                    ArrayList<String> showPhoto=new ArrayList<String>();
                    for(int i=0;i<mImageinfos.size();i++){
                        showPhoto.add(mImageinfos.get(i).getImgUrl());

                    }
                    ActivityUtil.create(context).go(ImagePagerActivity.class)
                            .put(ImagePagerActivity.URLS,showPhoto)
                            .put(ImagePagerActivity.CURRENT_POSITION,position)
                            .start();
                }
            });
            rv_allphoto.setAdapter(allphotoAdapter);
        }
        if(getArguments()!=null){
            friendID=getArguments().getString("friendID");
        }
        getUserinfo();
    }
    /**
     * 获取个人信息页面
     *
     */
    private void getUserinfo(){
        String userid=SharedPreferencesUtils.getString(Constant.SP_USERID);
        Observable<ResultData<Userinfo>> observable = HttpManager.getUserinfo(userid,friendID);
        observable.subscribeOn(Schedulers.io())
                .doOnSubscribe(new Action0() {
                    @Override
                    public void call() {
                        showDialog();
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ResultDataSubscriber<Userinfo>(this) {
                    @Override
                    public void onSuccess(String msg, Userinfo userinfo) {
                        mSwipeRefreshLayout.setRefreshing(false);
                        setDataForUser(userinfo);

                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        mSwipeRefreshLayout.setRefreshing(false);
                    }
                });
    }
    @Override
    protected void onVisibleToUser() {
        addOnOffsetChangeListener();
    }

    @Override
    protected void onInvisibleToUser() {
        removeOnOffsetChangeListener();
    }
    public void setDataForUser(Userinfo userinfo){
        if(!TextUtils.isEmpty(userinfo.getBirthday())){
            tv_birthday_fragment.setText(userinfo.getBirthday());
        }
        Utils.systemErr("---userinfo---"+userinfo.toString());
        if(!TextUtils.isEmpty(userinfo.getIndustry())){
            tv_industry.setText(userinfo.getIndustry());
        }
        if(!TextUtils.isEmpty(userinfo.getCharacter())){
            tv_sign.setText(userinfo.getCharacter());
        }

        if(!TextUtils.isEmpty(userinfo.getBelongLabel())){
            tv_belonglable.setText(userinfo.getBelongLabel());
            tv_belonglable.setVisibility(View.VISIBLE);
        }else{
            tv_belonglable.setVisibility(View.GONE);
        }
        malllabledatas.clear();
        if(!TextUtils.isEmpty(userinfo.getOtherLabel())){
            String[] sourceStrArray =userinfo.getOtherLabel().split(",");
            for(int i=0;i<sourceStrArray.length;i++){
                malllabledatas.add(new Lableinfo(sourceStrArray[i],false));
            }

        }
        alllableAdapter.notifyDataSetChanged();
        mImageinfos.clear();
        if(userinfo.getImgList()!=null&&userinfo.getImgList().size()!=0){
            mImageinfos.addAll(userinfo.getImgList());
        }
        allphotoAdapter.notifyDataSetChanged();
        if(userinfo.getApproveList()!=null&&userinfo.getApproveList().size()!=0){
            for(int i=0;i<userinfo.getApproveList().size();i++){
                if("A".equals(userinfo.getApproveList().get(i))){
                    iv_approve1.setVisibility(View.VISIBLE);
                    if("1".equals(userinfo.getOwnerAuthType())){
                        iv_approve1.setImageResource(R.mipmap.ic_havehouse1);
                    }else if ("2".equals(userinfo.getOwnerAuthType())){
                        iv_approve1.setImageResource(R.mipmap.ic_havehouse2);
                    }else{
                        iv_approve1.setImageResource(R.mipmap.ic_havehouse0);
                    }
                }else if("B".equals(userinfo.getApproveList().get(i))){
                    iv_approve2.setVisibility(View.VISIBLE);
                }else if("C".equals(userinfo.getApproveList().get(i))){
                    iv_approve3.setVisibility(View.VISIBLE);
                }else if("D".equals(userinfo.getApproveList().get(i))){
                    iv_approve4.setVisibility(View.VISIBLE);
                }
            }
        }else{
            iv_approve1.setVisibility(View.GONE);
            iv_approve2.setVisibility(View.GONE);
            iv_approve3.setVisibility(View.GONE);
            iv_approve4.setVisibility(View.GONE);
        }
    }

    void addOnOffsetChangeListener(){
        if (getActivity() instanceof AllUserInfoActivity) {
            ((AllUserInfoActivity) getActivity()).addOnOffsetChangedListener(this);
        }
    }

    void removeOnOffsetChangeListener(){
        if (getActivity() instanceof AllUserInfoActivity) {
            ((AllUserInfoActivity) getActivity()).removeOnOffsetChangedListener(this);
        }
    }

    @Override
    public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
        if (verticalOffset >= 0) {
            mSwipeRefreshLayout.setEnabled(true);
        } else {
            mSwipeRefreshLayout.setEnabled(false);
        }
    }
}
